package homework5


import akka.actor.{ActorRef, ActorSystem, Props}
import homework4.{AActor, BActor}

import java.util.{Timer, TimerTask}
import scala.collection.mutable.Map

object Main {


  val myFactory = ActorSystem("myFactory")
  val masterActorRef = myFactory.actorOf(Props[MasterActor], "masterActor")
  def main(args: Array[String]): Unit = {
    val map = MasterActor.map
    for (i <- 1 to 5 ) {
      val name = s"workerActor$i"
      val workerActorRef = myFactory.actorOf(Props[WorkerActor], name)
      workerActorRef ! ("注册", name, workerActorRef, masterActorRef)
      workerActorRef ! ("心跳", i * 1000, name, workerActorRef, masterActorRef)
    }
    val timer = new Timer()
    timer.schedule(new TimerTask {
      override def run(): Unit = {

        println("活跃的worker为:")
        map.foreach{ item =>
          if (item._2 <= System.currentTimeMillis() - 2000) {
            map.remove(item._1)
            println(s"${item._1}心跳过期")
            return
          }
          println(s"${item._1 }")
        }
      }
    },1000,1000)



  }
}
